program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var a,b : array of integer;
  n,i,t : integer;
  fin : textfile;

procedure Up(v:integer);
var x:integer;
begin
  while v>1 do begin
    if a[v]<a[v div 2] then begin
      x:=a[v];
      a[v]:=a[v div 2];
      a[v div 2]:=x;
      V:=v div 2;
    end else break;
  end;
end;


procedure Down(v:integer);
var x:integer;
begin
  while v<=n do begin
    if (a[v]>a[v +v +1])and(v+v+1<=n)and(a[v+v+1]<a[v+v]) then begin
      x:=a[v];
      a[v]:=a[v+v+1];
      a[v+v+1]:=x;
      v:=v+v+1;
    end else
      if (a[v]>a[v +v])and(v+v<=n) then begin
        x:=a[v];
        a[v]:=a[v+v];
        a[v+v]:=x;
        v:=v+v;
      end else break;
  end;
end;
begin
readln(n);
t:=n;
for i:=1 to n do begin read(a[i]);
write(a[i],' ');
UP(i);
end;
while n>0 do begin
  b[t-n+1]:=a[1];
  a[1]:=a[n];
  n:=n-1;
  down(1);
end;
for i:=0 to 2 do
writeln;
for i:=1 to t do
  write( b[i],' ');
  readln;
end.

